﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing.Printing;
using System.Data;
using Model;
using System.Windows.Forms;
using System.Drawing;
using System.Xml;
using BLL;

namespace Sales
{
    public class POSPrintDocument
    {
        public PrintDocument printDocument1 = new PrintDocument();
        private static XmlModel xmlModel;

        public Common2 common = new Common2();



        private static DataTable spInfoData;

        #region 页面属性

        public string Khmc
        {
            get;
            set;
        }
        /// <summary>
        /// 收银员
        /// </summary>
        public string Syymc
        {
            get;
            set;
        }
        /// <summary>
        /// 营业员
        /// </summary>
        public string Yyymc
        {
            get;
            set;
        }

        /// <summary>
        /// 店面
        /// </summary>
        public string Dmmc
        {
            get;
            set;
        }

        /// <summary>
        /// 会员卡消费
        /// </summary>
        public string Hykxf
        {
            get;
            set;
        }

        /// <summary>
        /// 现金消费
        /// </summary>
        public string Xj
        {
            get;
            set;
        }

        public string Wx
        {
            get;
            set;
        }

        public string Zfb
        {
            get;
            set;
        }

        public string Yhk
        {
            get;
            set;
        }

        public string Djq
        {
            get;
            set;
        }



        public string Ys
        {
            get;
            set;
        }

        public string Ss
        {
            get;
            set;
        }

        public string Zl
        {
            get;
            set;
        }
        /// <summary>
        /// 折扣
        /// </summary>
        public string Zk { get; set; }
        /// <summary>
        /// 卡号
        /// </summary>
        public string Kh
        {
            get;
            set;
        }
        /// <summary>
        /// 充值金额
        /// </summary>
        public string Czje
        {
            get;
            set;
        }
        /// <summary>
        /// 赠送金额
        /// </summary>
        public string Zsje
        {
            get;
            set;
        }
        /// <summary>
        /// 销售单号
        /// </summary>
        public string Xsdh { get; set; }

        /// <summary>
        /// 最后余额
        /// </summary>
        public decimal Zhye { get; set; }

        #endregion

        #region 打印条形码

        private static string NewYyymc = "";

        /// <summary>
        /// 打印票据
        /// </summary>
        /// <param name="spdahdt">打印数据源</param>
        /// <param name="khmc">客户名称</param>
        /// <param name="syymc">收银员名称</param>
        /// <param name="yyymc">营业员名称</param>
        public void PrintXP(DataTable spdahdt, string khmc, string syymc, string yyymc, string dmmc, string hykje, string ssxj, string ysxj, string zl)
        {
            // printDocument1 为 打印控件

            //  xmlModel = GetXmlModel();


            int width = 400;
            int hight = 500;
            if (xmlModel != null)
            {
                width = int.Parse(xmlModel.PageWidth);
                hight = int.Parse(xmlModel.PageHeight);
            }


            this.printDocument1.DefaultPageSettings.PaperSize = new PaperSize("Custum", width, hight);

            //根据对话框中输入的页边距设置打印文档的页边距
            printDocument1.DefaultPageSettings.Margins = new Margins(4, 20, 4, 20);            //设置打印文档的页面大小
            this.printDocument1.PrintPage += new PrintPageEventHandler(printDocument1_PrintPage);
            // this.printDocument1.PrintPage += new PrintPageEventHandler(printDocument4_PrintPage);
            spInfoData = spdahdt;
            Khmc = khmc;
            Syymc = syymc;
            NewYyymc = yyymc;
            Yyymc = yyymc;
            Hykxf = hykje;
            Dmmc = dmmc;
            if (hykje != "")
            {
                Hykxf = decimal.Parse(hykje).ToString("0.00");
            }

            if (ssxj != "")
            {
                Xj = decimal.Parse(ssxj).ToString("0.00");
            }

            if (ysxj != "")
            {
                Ys = decimal.Parse(ysxj).ToString("0.00");
            }
            if (zl != "")
            {
                Zl = decimal.Parse(zl).ToString("0.00");
            }


            bool status = false;
            DataTable dt = ChanageData();
            foreach (DataRow dr in dt.Rows)
            {
                int number = 0;
                string name = dr["名称"].ToString();
                if (dr["次数"].ToString() != "")
                {
                    number = int.Parse(dr["次数"].ToString());
                }
                this.printDocument1.DefaultPageSettings.PrinterSettings.PrinterName = name;
                for (int i = 0; i < number; i++)
                {
                    try
                    {
                        this.printDocument1.Print();


                        //// 打印预览
                        //PrintPreviewDialog _Dialog = new PrintPreviewDialog();
                        //_Dialog.Document = this.printDocument1;
                        //_Dialog.ShowDialog();
                    }
                    catch (Exception ex)
                    {
                        Message.Warning(ex.ToString());
                        status = true;
                        break;
                    }
                }
                if (status)
                {
                    break;
                }
            }

        }


        public void PrintXP(DataTable spdahdt, Hy_Entity hyEntity, LoginInfo loginInfo, Sale SaleEntity)
        {
            // printDocument1 为 打印控件

            xmlModel = common.GetPosConfig();// GetXmlModel();


            int width = 400;
            int hight = 500;
            try
            {
                width = int.Parse(xmlModel.PageWidth);
                hight = int.Parse(xmlModel.PageHeight);

                this.Xsdh = SaleEntity.Xsdh;//销售单号

            }
            catch { }

            this.printDocument1.DefaultPageSettings.PaperSize = new PaperSize("Custum", width, hight);

            //根据对话框中输入的页边距设置打印文档的页边距
            printDocument1.DefaultPageSettings.Margins = new Margins(4, 20, 4, 20);            //设置打印文档的页面大小
            this.printDocument1.PrintPage += new PrintPageEventHandler(printDocument1_PrintPage);
            // this.printDocument1.PrintPage += new PrintPageEventHandler(printDocument4_PrintPage);
            spInfoData = spdahdt;
            Khmc = hyEntity.Hymc;
            Syymc = loginInfo.Syybh;
            NewYyymc = loginInfo.Yyymc;
            Yyymc = loginInfo.Yyymc;
            Hykxf = SaleEntity.Hykxf;
            Dmmc = loginInfo.Dmmc;
            this.Zhye = SaleEntity.Zhye;

            //if (hykje != "")
            //{
            //    Hykxf = decimal.Parse(hykje).ToString("0.00");
            //}
            //实收
            if (SaleEntity.Ss != null && SaleEntity.Ss != "")
            {
                Ss = decimal.Parse(SaleEntity.Ss).ToString("0.00");
            }
            //现金
            if (SaleEntity.Xj != null && SaleEntity.Xj != "")
            {
                Xj = decimal.Parse(SaleEntity.Xj).ToString("0.00");
            }
            //微信
            if (SaleEntity.Wx != null && SaleEntity.Wx != "")
            {
                Wx = decimal.Parse(SaleEntity.Wx).ToString("0.00");
            }


            //支付宝
            if (SaleEntity.Zfb != null && SaleEntity.Zfb != "")
            {
                Zfb = decimal.Parse(SaleEntity.Zfb).ToString("0.00");
            }

            //银行卡
            if (SaleEntity.Yhk != null && SaleEntity.Yhk != "")
            {
                Yhk = decimal.Parse(SaleEntity.Yhk).ToString("0.00");
            }


            //代金券
            if (SaleEntity.Djq != null && SaleEntity.Djq != "")
            {
                Djq = decimal.Parse(SaleEntity.Djq).ToString("0.00");
            }


            //应收
            if (SaleEntity.Ys != null && SaleEntity.Ys != "")
            {
                Ys = decimal.Parse(SaleEntity.Ys).ToString("0.00");
            }
            //找零
            if (SaleEntity.Zl != null && SaleEntity.Zl != "")
            {
                Zl = decimal.Parse(SaleEntity.Zl).ToString("0.00");
            }

            //找零
            if (SaleEntity.DZk != 0)
            {
                Zk = SaleEntity.DZk.ToString("0.00");
            }



            bool status = false;
            DataTable dt = ChanageData();
            foreach (DataRow dr in dt.Rows)
            {
                int number = 0;
                string name = dr["名称"].ToString();
                if (dr["次数"].ToString() != "")
                {
                    number = int.Parse(dr["次数"].ToString());
                }
                this.printDocument1.DefaultPageSettings.PrinterSettings.PrinterName = name;
                for (int i = 0; i < number; i++)
                {
                    try
                    {
                        this.printDocument1.Print();
                    }
                    catch (Exception ex)
                    {
                        Message.Warning(ex.ToString());
                        status = true;
                        break;
                    }
                }
                if (status)
                {
                    break;
                }
            }

        }





        private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
        {

            if (xmlModel != null)
            {
                string titleName = xmlModel.Title;

                string dmmc = "";
                if (xmlModel.PrintName == "1")
                    dmmc = Dmmc;

                int seat = 12;//位置

                /*如果需要改变自己 可以在new Font(new FontFamily("黑体"),11）中的“黑体”改成自己要的字体就行了，黑体 后面的数字代表字体的大小*/
                e.Graphics.DrawString(titleName, new Font(new FontFamily("黑体"), 13), System.Drawing.Brushes.Black, 30, 10);
                e.Graphics.DrawString(dmmc, new Font(new FontFamily("宋体"), 8), System.Drawing.Brushes.Blue, 140, 20);
                e.Graphics.DrawString("销售单号: " + this.Xsdh, new Font(new FontFamily("宋体"), 8), System.Drawing.Brushes.Black, seat, 35);

                //e.Graphics.DrawString("客户名称：" + Khmc, new Font(new FontFamily("黑体"), 9), System.Drawing.Brushes.Blue, 15, 30);

                //if (Khmc != "")
                //{
                //    e.Graphics.DrawString("客户名称：" + Khmc, new Font(new FontFamily("黑体"), 9), System.Drawing.Brushes.Blue, 15, 30);

                //}

                //项目的名称

                int line = 200;

                if (xmlModel.PageWidth != "" && ValidationHelper.IsInt(xmlModel.PageWidth))
                    line = int.Parse(xmlModel.PageWidth) - 10;


                e.Graphics.DrawLine(Pens.Black, 8, 50, line, 50);


                if (xmlModel.ID == "1")
                {
                    int topWidth = 23;
                    if (ValidationHelper.IsInt(xmlModel.IDWidth))
                        topWidth = int.Parse(xmlModel.IDWidth);
                    e.Graphics.DrawString("序号", new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, seat, 52);
                    seat += topWidth;//序号的宽度 23
                }
                if (xmlModel.Spbh == "1")
                {
                    int topWidth = 35;

                    if (ValidationHelper.IsInt(xmlModel.SpbhWidth))
                        topWidth = int.Parse(xmlModel.SpbhWidth);

                    e.Graphics.DrawString("商品编号", new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, seat, 52);

                    seat += topWidth;//商品编号宽度 35
                }
                if (xmlModel.Spmc == "1")
                {
                    int topWidth = 65;
                    if (ValidationHelper.IsInt(xmlModel.SpmcWidth))
                        topWidth = int.Parse(xmlModel.SpmcWidth);

                    e.Graphics.DrawString("商品名称", new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, seat, 52);
                    seat += topWidth;//商品名称宽度 65
                }
                if (xmlModel.Sl == "1")
                {
                    int topWidth = 23;

                    if (ValidationHelper.IsInt(xmlModel.SlWidth))
                        topWidth = int.Parse(xmlModel.SlWidth);


                    e.Graphics.DrawString("数量", new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, seat, 52);
                    seat += topWidth;//数量 宽度 30
                }



                if (xmlModel.Dw == "1")
                {
                    int topWidth = 23;
                    if (ValidationHelper.IsInt(xmlModel.DwWidth))
                        topWidth = int.Parse(xmlModel.DwWidth);

                    e.Graphics.DrawString("单位", new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, seat, 52);
                    seat += topWidth;//数量 宽度 30
                }

                if (xmlModel.Gg == "1")
                {
                    int topWidth = 23;

                    if (ValidationHelper.IsInt(xmlModel.GgWidth))
                        topWidth = int.Parse(xmlModel.GgWidth);

                    e.Graphics.DrawString("规格", new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, seat, 52);
                    seat += topWidth;//数量 宽度 30
                }

                if (xmlModel.Zk == "1")
                {
                    int topWidth = 23;

                    if (ValidationHelper.IsInt(xmlModel.ZkWidth))
                        topWidth = int.Parse(xmlModel.ZkWidth);


                    SizeF zklSize = e.Graphics.MeasureString("折让", new Font(new FontFamily("宋体"), 9));
                    float zlSize = seat + topWidth - zklSize.Width + 20;


                    e.Graphics.DrawString("折让", new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, zlSize, 52);
                    seat += topWidth;//数量 宽度 30



                }
                if (xmlModel.Zhj == "1")
                {
                    int topWidth = 23;

                    if (ValidationHelper.IsInt(xmlModel.ZhjWidth))
                        topWidth = int.Parse(xmlModel.ZhjWidth);


                    SizeF zhjSize = e.Graphics.MeasureString("折后价", new Font(new FontFamily("宋体"), 9));
                    float zhSize = seat + topWidth - zhjSize.Width;


                    e.Graphics.DrawString("折后价", new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, zhSize, 52);
                    seat += topWidth;//数量 宽度 30
                }

                if (xmlModel.Pfj == "1")
                {
                    int topWidth = 23;

                    if (ValidationHelper.IsInt(xmlModel.PfjWidth))
                        topWidth = int.Parse(xmlModel.PfjWidth);

                    SizeF pfjSize = e.Graphics.MeasureString("批发价", new Font(new FontFamily("宋体"), 9));
                    float pSize = seat + topWidth - pfjSize.Width;



                    e.Graphics.DrawString("批发价", new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, pSize, 52);
                    seat += topWidth;//数量 宽度 30
                }
                ////////////////////////////


                if (xmlModel.Dj == "1")
                {
                    int topWidth = 30;


                    if (ValidationHelper.IsInt(xmlModel.DjWidth))
                        topWidth = int.Parse(xmlModel.DjWidth);


                    SizeF lsjSize = e.Graphics.MeasureString("单价", new Font(new FontFamily("宋体"), 9));
                    float jeSize = seat + topWidth - lsjSize.Width;


                    e.Graphics.DrawString("单价", new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, jeSize, 52);
                    seat += topWidth;//单价 宽度 30
                }
                if (xmlModel.Xj == "1")
                {
                    int topWidth = 30;

                    if (ValidationHelper.IsInt(xmlModel.XjWidth))
                        topWidth = int.Parse(xmlModel.XjWidth);



                    SizeF lsjSize = e.Graphics.MeasureString("金额", new Font(new FontFamily("宋体"), 9));
                    float x = seat + topWidth - lsjSize.Width;


                    e.Graphics.DrawString("金额", new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, x, 52);
                    seat += topWidth;//商品名称宽度 30
                }

                int rowIndex = 0;
                float high = 65;
                float slSite = 0;//数量位置
                float xjSite = 0;//现金位置

                foreach (DataRow dr in spInfoData.Rows)
                {
                    int index = rowIndex + 1;
                    //项目信息

                    seat = 12;//位置
                    if (xmlModel.ID == "1")
                    {
                        int lineWidth = 23;

                        try
                        {
                            lineWidth = int.Parse(xmlModel.IDWidth);
                        }
                        catch { }

                        e.Graphics.DrawString(index.ToString(), new Font(new FontFamily("宋体"), 9), System.Drawing.Brushes.Black, seat, high);
                        seat += lineWidth;//序号的宽度 默认 23
                    }
                    if (xmlModel.Spbh == "1")
                    {
                        int lineWidth = 65;

                        try
                        {
                            lineWidth = int.Parse(xmlModel.SpbhWidth);
                        }
                        catch { }

                        e.Graphics.DrawString(dr["spbh"].ToString(), new Font(new FontFamily("宋体"), 9), System.Drawing.Brushes.Black, seat, high);

                        seat += lineWidth;//商品编号宽度 65
                    }
                    if (xmlModel.Spmc == "1")
                    {
                        int lineWidth = 65;

                        try
                        {
                            lineWidth = int.Parse(xmlModel.SpmcWidth);
                        }
                        catch { }

                        e.Graphics.DrawString(dr["spmc"].ToString(), new Font(new FontFamily("宋体"), 9), System.Drawing.Brushes.Black, seat, high);
                        seat += lineWidth;//商品名称宽度 65
                    }
                    if (xmlModel.Sl == "1")
                    {
                        int lineWidth = 30;

                        try
                        {
                            lineWidth = int.Parse(xmlModel.SlWidth);
                        }
                        catch { }

                        e.Graphics.DrawString(dr["sl"].ToString() + dr["dw"].ToString(), new Font(new FontFamily("宋体"), 9), System.Drawing.Brushes.Black, seat, high);
                        slSite = seat;

                        seat += lineWidth;//数量 宽度 30
                    }


                    /////////////////////添加新字段 dw，gg，zk，zhj，pfj
                    if (xmlModel.Dw == "1")
                    {
                        int lineWidth = 30;

                        try
                        {
                            lineWidth = int.Parse(xmlModel.DwWidth);
                        }
                        catch { }

                        e.Graphics.DrawString(dr["dw"].ToString(), new Font(new FontFamily("宋体"), 9), System.Drawing.Brushes.Black, seat, high);
                        slSite = seat;
                        seat += lineWidth;//数量 宽度 30
                    }

                    if (xmlModel.Gg == "1")
                    {
                        int lineWidth = 30;

                        try
                        {
                            lineWidth = int.Parse(xmlModel.GgWidth);
                        }
                        catch { }

                        e.Graphics.DrawString(dr["gg"].ToString(), new Font(new FontFamily("宋体"), 9), System.Drawing.Brushes.Black, seat, high);
                        slSite = seat;
                        seat += lineWidth;//数量 宽度 30
                    }

                    if (xmlModel.Zk == "1")
                    {
                        int lineWidth = 30;

                        try
                        {
                            lineWidth = int.Parse(xmlModel.ZkWidth);
                        }
                        catch { }

                        string zkl = double.Parse(dr["zkl"].ToString()).ToString("0.");

                        SizeF zklSize = e.Graphics.MeasureString(zkl, new Font(new FontFamily("宋体"), 9));
                        float fzklSize = seat + lineWidth - zklSize.Width;


                        e.Graphics.DrawString(dr["zkl"].ToString(), new Font(new FontFamily("宋体"), 9), System.Drawing.Brushes.Black, fzklSize, high);
                        slSite = seat;
                        seat += lineWidth;//数量 宽度 30
                    }

                    if (xmlModel.Zhj == "1")
                    {
                        int lineWidth = 30;

                        try
                        {
                            lineWidth = int.Parse(xmlModel.ZhjWidth);
                        }
                        catch { }

                        string zhj = double.Parse(dr["zhj"].ToString()).ToString("0.");

                        SizeF zhjSize = e.Graphics.MeasureString(zhj, new Font(new FontFamily("宋体"), 9));
                        float fzhjSize = seat + lineWidth - zhjSize.Width;


                        e.Graphics.DrawString(zhj, new Font(new FontFamily("宋体"), 9), System.Drawing.Brushes.Black, fzhjSize, high);
                        slSite = seat;
                        seat += lineWidth;//数量 宽度 30
                    }

                    if (xmlModel.Pfj == "1")
                    {
                        int lineWidth = 30;

                        try
                        {
                            lineWidth = int.Parse(xmlModel.PfjWidth);
                        }
                        catch { }

                        string pfj = double.Parse(dr["pfj"].ToString()).ToString("0.");

                        SizeF pfjSize = e.Graphics.MeasureString(pfj, new Font(new FontFamily("宋体"), 9));
                        float fpfjSize = seat + lineWidth - pfjSize.Width;


                        e.Graphics.DrawString(pfj, new Font(new FontFamily("宋体"), 9), System.Drawing.Brushes.Black, fpfjSize, high);
                        // slSite = seat;
                        seat += lineWidth;//数量 宽度 30
                    }
                    /////////////////////

                    if (xmlModel.Dj == "1")
                    {
                        int lineWidth = 30;

                        try
                        {
                            lineWidth = int.Parse(xmlModel.DjWidth);
                        }
                        catch { }

                        // e.Graphics.DrawString(dr["lsj"].ToString(), new Font(new FontFamily("宋体"), 9), System.Drawing.Brushes.Black, seat, high);
                        string lsj = double.Parse(dr["lsj"].ToString()).ToString("0.");
                        SizeF lsjSize = e.Graphics.MeasureString(lsj, new Font(new FontFamily("宋体"), 9));
                        float x1 = seat + lineWidth - lsjSize.Width;
                        e.Graphics.DrawString(lsj, new Font(new FontFamily("宋体"), 9), System.Drawing.Brushes.Black, x1, high);


                        seat += lineWidth;//单价 宽度 30
                    }
                    if (xmlModel.Xj == "1")
                    {
                        int lineWidth = 30;

                        try
                        {
                            lineWidth = int.Parse(xmlModel.XjWidth);
                        }
                        catch { }

                        //   e.Graphics.DrawString(dr["ssje"].ToString(), new Font(new FontFamily("宋体"), 9), System.Drawing.Brushes.Black, seat, high);

                        string ssje = double.Parse(dr["ssje"].ToString()).ToString("0.");

                        //if (ssje.IndexOf('.') == -1)
                        //{
                        //    ssje = ssje;
                        //}
                        SizeF ssjeSize = e.Graphics.MeasureString(ssje, new Font(new FontFamily("宋体"), 9));
                        float x3 = seat + lineWidth - ssjeSize.Width;
                        e.Graphics.DrawString(ssje, new Font(new FontFamily("宋体"), 9), System.Drawing.Brushes.Black, x3, high);



                        xjSite = x3;
                        seat += lineWidth;//商品名称宽度 30
                    }

                    high += 13;

                    rowIndex++;
                }

                e.Graphics.DrawString("合计：", new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, 12, high);//合计行

                //  e.Graphics.DrawString(GetAllSl(spInfoData).ToString(), new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, slSite, high);
                e.Graphics.DrawString(GetAllMoney(spInfoData).ToString("0.0"), new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, xjSite, high);



                high += 13;

                e.Graphics.DrawLine(Pens.Black, 8, high, line, high);
                high = high + 10;//显示下分割线


                int key = 0;

                decimal _hykxf = 0;
                decimal _SS = 0;
                //会员卡消费
                if (Hykxf != null && Hykxf != "")
                    _hykxf = decimal.Parse(Hykxf);
                if (Ss != null && Ss != "")
                    _SS = decimal.Parse(Ss);

                _SS = _SS + _hykxf;//包括会员卡消费
                if (_SS != 0)
                {
                    high = high + 20;//付现
                    e.Graphics.DrawString("实付：" + _SS.ToString("0.0"), new Font(new FontFamily("黑体"), 12), System.Drawing.Brushes.Black, 13, high);
                    key++;
                }

                int iHigh = 0;
                //具体支付方式
                string fkfs = GetZffs(ref iHigh);
                if (fkfs != "")
                {
                    high = high + 20;//付现
                    e.Graphics.DrawString("支付方式：" + fkfs, new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, 13, high);
                    //  key++;
                }

                high = high + iHigh + 5;
                string Glsf = "";//共计收费

                if (Hykxf != null && Hykxf != "")
                {
                    high = high + 20;//会员卡消费
                    e.Graphics.DrawString("会员：" + Khmc, new Font(new FontFamily("黑体"), 12), System.Drawing.Brushes.Black, 13, high);
                    Glsf = (decimal.Parse(Hykxf) + decimal.Parse(Xj)).ToString();
                    key++;

                    high = high + 20;//会员卡消费
                    e.Graphics.DrawString("会员卡消费：" + Hykxf, new Font(new FontFamily("黑体"), 12), System.Drawing.Brushes.Black, 13, high);
                    Glsf = (decimal.Parse(Hykxf) + decimal.Parse(Xj)).ToString();
                    key++;

                    high = high + 20;//会员卡消费
                    e.Graphics.DrawString("会员卡余额：" + this.Zhye.ToString("0.0"), new Font(new FontFamily("宋体"), 8), System.Drawing.Brushes.Black, 13, high);
                    Glsf = (decimal.Parse(Hykxf) + decimal.Parse(Xj)).ToString();
                    key++;



                }

                //if (key == 2)
                //{
                //    high = high + 20;//共计
                //    e.Graphics.DrawString("共计：" + decimal.Parse(Glsf).ToString("0.0"), new Font(new FontFamily("黑体"), 12), System.Drawing.Brushes.Black, 13, high);
                //}


                if (Zk != null && Zk != "")
                {
                    high = high + 20;//找零
                    e.Graphics.DrawString("折让：" + Zk, new Font(new FontFamily("黑体"), 12), System.Drawing.Brushes.Black, 13, high);
                }


                if (Zl != null && Zl != "0.00")
                {
                    high = high + 20;//找零
                    e.Graphics.DrawString("找零：" + decimal.Parse(Zl).ToString("0.0"), new Font(new FontFamily("黑体"), 12), System.Drawing.Brushes.Black, 13, high);
                }

                high = high + 20;//收银员
                e.Graphics.DrawString("收银员：" + Syymc, new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, 13, high);

                // e.Graphics.DrawString("营业员：" + NewYyymc, new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, 200, high);

                string yyyStr = "营业员：" + NewYyymc;

                SizeF yyyStrSize = e.Graphics.MeasureString(yyyStr, new Font(new FontFamily("宋体"), 8));
                float yyySize = line - yyyStrSize.Width;

                e.Graphics.DrawString(yyyStr, new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, yyySize, high);

                if (xmlModel.PrintTime == "1")
                {
                    high = high + 15;//时间

                    e.Graphics.DrawString("时间：" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"), new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, 12, high);
                }


                if (xmlModel.ctxtYw1 != "")
                {
                    high = high + 15;//时间
                    int width = GetYWSite(xmlModel.key1);
                    e.Graphics.DrawString(xmlModel.ctxtYw1, new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, width, high);

                }
                if (xmlModel.ctxtYw2 != "")
                {
                    high = high + 15;//时间
                    int width = GetYWSite(xmlModel.key2);
                    e.Graphics.DrawString(xmlModel.ctxtYw2, new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, width, high);
                }
                if (xmlModel.ctxtYw3 != "")
                {
                    high = high + 15;//时间
                    int width = GetYWSite(xmlModel.key3);
                    e.Graphics.DrawString(xmlModel.ctxtYw3, new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, width, high);
                }
                if (xmlModel.ctxtYw4 != "")
                {
                    high = high + 15;//时间
                    int width = GetYWSite(xmlModel.key4);
                    e.Graphics.DrawString(xmlModel.ctxtYw4, new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, width, high);
                }
                if (xmlModel.ctxtYw5 != "")
                {
                    high = high + 15;//时间
                    int width = GetYWSite(xmlModel.key5);
                    e.Graphics.DrawString(xmlModel.ctxtYw5, new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, width, high);
                }
                if (xmlModel.ctxtYw6 != "")
                {
                    high = high + 15;//时间
                    int width = GetYWSite(xmlModel.key6);
                    e.Graphics.DrawString(xmlModel.ctxtYw6, new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, width, high);
                }

            }

        }

        private void printDocument4_PrintPage(object sender, PrintPageEventArgs e)
        {
            try
            {


                string titleName = xmlModel.Title;

                string dmmc = "";

                if (xmlModel.PrintName == "1")
                    dmmc = Dmmc;




                /*如果需要改变自己 可以在new Font(new FontFamily("黑体"),11）中的“黑体”改成自己要的字体就行了，黑体 后面的数字代表字体的大小
              System.Drawing.Brushes.Blue , 170, 10 中的 System.Drawing.Brushes.Blue 为颜色，后面的为输出的位置 */
                e.Graphics.DrawString(titleName, new Font(new FontFamily("宋体"), 13), System.Drawing.Brushes.Black, 30, 10);
                e.Graphics.DrawString(dmmc, new Font(new FontFamily("宋体"), 8), System.Drawing.Brushes.Blue, 125, 30);
                //e.Graphics.DrawString("客户名称：" + Khmc, new Font(new FontFamily("黑体"), 9), System.Drawing.Brushes.Blue, 15, 30);

                //if (Khmc != "")
                //{
                //    e.Graphics.DrawString("客户名称：" + Khmc, new Font(new FontFamily("黑体"), 9), System.Drawing.Brushes.Blue, 15, 30);

                //}

                //项目的名称

                int line = 200;

                if (xmlModel.PageWidth != "")
                {
                    try
                    {
                        line = int.Parse(xmlModel.PageWidth) - 10;
                    }
                    catch { }
                }

                e.Graphics.DrawLine(Pens.Black, 8, 50, line, 50);

                int seat = 12;//位置
                if (xmlModel.ID == "1")
                {
                    int topWidth = 23;

                    try
                    {
                        topWidth = int.Parse(xmlModel.IDWidth);
                    }
                    catch { }

                    e.Graphics.DrawString("序号", new Font(new FontFamily("宋体"), 8, FontStyle.Bold), System.Drawing.Brushes.Black, seat, 52);
                    seat += topWidth;//序号的宽度 23
                }

                if (xmlModel.Spmc == "1")
                {
                    int topWidth = 65;
                    try
                    {
                        topWidth = int.Parse(xmlModel.SpmcWidth);
                    }
                    catch { }

                    e.Graphics.DrawString("名称", new Font(new FontFamily("宋体"), 8, FontStyle.Bold), System.Drawing.Brushes.Black, seat, 52);
                    seat += topWidth;//商品名称宽度 65
                }


                if (xmlModel.Spbh == "1")
                {
                    int topWidth = 35;

                    try
                    {
                        topWidth = int.Parse(xmlModel.SpbhWidth);
                    }
                    catch { }

                    e.Graphics.DrawString("货号", new Font(new FontFamily("宋体"), 8, FontStyle.Bold), System.Drawing.Brushes.Black, seat, 52);

                    seat += topWidth;//商品编号宽度 35
                }

                if (xmlModel.Sl == "1")
                {
                    int topWidth = 23;

                    try
                    {
                        topWidth = int.Parse(xmlModel.SlWidth);
                    }
                    catch { }

                    e.Graphics.DrawString("数量", new Font(new FontFamily("宋体"), 8, FontStyle.Bold), System.Drawing.Brushes.Black, seat, 52);
                    seat += topWidth;//数量 宽度 30
                }

                ////////////////////////////添加新字段 dw，gg，zk，zhj，pfj

                if (xmlModel.Dw == "1")
                {
                    int topWidth = 23;

                    try
                    {
                        topWidth = int.Parse(xmlModel.DwWidth);
                    }
                    catch { }

                    e.Graphics.DrawString("单位", new Font(new FontFamily("宋体"), 8, FontStyle.Bold), System.Drawing.Brushes.Black, seat, 52);
                    seat += topWidth;//数量 宽度 30
                }

                if (xmlModel.Gg == "1")
                {
                    int topWidth = 23;

                    try
                    {
                        topWidth = int.Parse(xmlModel.GgWidth);
                    }
                    catch { }

                    e.Graphics.DrawString("规格", new Font(new FontFamily("宋体"), 8, FontStyle.Bold), System.Drawing.Brushes.Black, seat, 52);
                    seat += topWidth;//数量 宽度 30
                }

                if (xmlModel.Zk == "1")
                {
                    int topWidth = 23;

                    try
                    {
                        topWidth = int.Parse(xmlModel.ZkWidth);
                    }
                    catch { }

                    SizeF zklSize = e.Graphics.MeasureString("折让", new Font(new FontFamily("宋体"), 9, FontStyle.Bold));
                    float zlSize = seat + topWidth - zklSize.Width + 20;


                    e.Graphics.DrawString("折让", new Font(new FontFamily("宋体"), 8), System.Drawing.Brushes.Black, zlSize, 52);
                    seat += topWidth;//数量 宽度 30



                }
                if (xmlModel.Zhj == "1")
                {
                    int topWidth = 23;

                    try
                    {
                        topWidth = int.Parse(xmlModel.ZhjWidth);
                    }
                    catch { }

                    SizeF zhjSize = e.Graphics.MeasureString("折后价", new Font(new FontFamily("宋体"), 9, FontStyle.Bold));
                    float zhSize = seat + topWidth - zhjSize.Width;


                    e.Graphics.DrawString("折后价", new Font(new FontFamily("宋体"), 9, FontStyle.Bold), System.Drawing.Brushes.Black, zhSize, 52);
                    seat += topWidth;//数量 宽度 30
                }

                if (xmlModel.Pfj == "1")
                {
                    int topWidth = 23;

                    try
                    {
                        topWidth = int.Parse(xmlModel.PfjWidth);
                    }
                    catch { }

                    SizeF pfjSize = e.Graphics.MeasureString("批发价", new Font(new FontFamily("宋体"), 9, FontStyle.Bold));
                    float pSize = seat + topWidth - pfjSize.Width;



                    e.Graphics.DrawString("批发价", new Font(new FontFamily("宋体"), 9, FontStyle.Bold), System.Drawing.Brushes.Black, pSize, 52);
                    seat += topWidth;//数量 宽度 30
                }
                ////////////////////////////


                if (xmlModel.Dj == "1")
                {
                    int topWidth = 30;

                    try
                    {
                        topWidth = int.Parse(xmlModel.DjWidth);
                    }
                    catch { }

                    SizeF lsjSize = e.Graphics.MeasureString("单价", new Font(new FontFamily("宋体"), 9, FontStyle.Bold));
                    float jeSize = seat + topWidth - lsjSize.Width;


                    e.Graphics.DrawString("单价", new Font(new FontFamily("宋体"), 9, FontStyle.Bold), System.Drawing.Brushes.Black, jeSize, 52);
                    seat += topWidth;//单价 宽度 30
                }
                if (xmlModel.Xj == "1")
                {
                    int topWidth = 30;

                    try
                    {
                        topWidth = int.Parse(xmlModel.XjWidth);
                    }
                    catch { }


                    SizeF lsjSize = e.Graphics.MeasureString("金额", new Font(new FontFamily("宋体"), 9, FontStyle.Bold));
                    float x = seat + topWidth - lsjSize.Width;


                    e.Graphics.DrawString("金额", new Font(new FontFamily("宋体"), 9, FontStyle.Bold), System.Drawing.Brushes.Black, x, 52);
                    seat += topWidth;//商品名称宽度 30
                }

                int rowIndex = 0;
                float high = 65;
                float slSite = 0;//数量位置
                float xjSite = 0;//现金位置

                foreach (DataRow dr in spInfoData.Rows)
                {
                    int index = rowIndex + 1;
                    //项目信息

                    seat = 12;//位置
                    if (xmlModel.ID == "1")
                    {
                        int lineWidth = 23;

                        try
                        {
                            lineWidth = int.Parse(xmlModel.IDWidth);
                        }
                        catch { }

                        e.Graphics.DrawString(index.ToString(), new Font(new FontFamily("宋体"), 9), System.Drawing.Brushes.Black, seat, high);
                        seat += lineWidth;//序号的宽度 默认 23
                    }

                    if (xmlModel.Spmc == "1")
                    {
                        int lineWidth = 65;

                        try
                        {
                            lineWidth = int.Parse(xmlModel.SpmcWidth);
                        }
                        catch { }


                        string dlmc = GetSpDlmc(dr["spbh"].ToString());
                        LogHelper.m_CreateErrorLogTxt("商品名称", "1", "大类名称:" + dlmc + "坐标:" + seat + "," + high);


                        e.Graphics.DrawString(dlmc, new Font(new FontFamily("宋体"), 9), System.Drawing.Brushes.Black, seat, high);
                        seat += lineWidth;//商品名称宽度 65
                    }

                    if (xmlModel.Spbh == "1")
                    {
                        int lineWidth = 65;

                        try
                        {
                            lineWidth = int.Parse(xmlModel.SpbhWidth);
                        }
                        catch { }


                        string sphh = GetSpHh(dr["spbh"].ToString());
                        e.Graphics.DrawString(sphh, new Font(new FontFamily("宋体"), 9), System.Drawing.Brushes.Black, seat, high);

                        seat += lineWidth;//商品编号宽度 65
                    }


                    if (xmlModel.Sl == "1")
                    {
                        int lineWidth = 30;

                        try
                        {
                            lineWidth = int.Parse(xmlModel.SlWidth);
                        }
                        catch { }

                        e.Graphics.DrawString(dr["sl"].ToString() + dr["dw"].ToString(), new Font(new FontFamily("宋体"), 9), System.Drawing.Brushes.Black, seat, high);
                        slSite = seat;

                        seat += lineWidth;//数量 宽度 30
                    }


                    /////////////////////添加新字段 dw，gg，zk，zhj，pfj
                    if (xmlModel.Dw == "1")
                    {
                        int lineWidth = 30;

                        try
                        {
                            lineWidth = int.Parse(xmlModel.DwWidth);
                        }
                        catch { }

                        e.Graphics.DrawString(dr["dw"].ToString(), new Font(new FontFamily("宋体"), 9), System.Drawing.Brushes.Black, seat, high);
                        slSite = seat;
                        seat += lineWidth;//数量 宽度 30
                    }

                    if (xmlModel.Gg == "1")
                    {
                        int lineWidth = 30;

                        try
                        {
                            lineWidth = int.Parse(xmlModel.GgWidth);
                        }
                        catch { }

                        e.Graphics.DrawString(dr["gg"].ToString(), new Font(new FontFamily("宋体"), 9), System.Drawing.Brushes.Black, seat, high);
                        slSite = seat;
                        seat += lineWidth;//数量 宽度 30
                    }

                    if (xmlModel.Zk == "1")
                    {
                        int lineWidth = 30;

                        try
                        {
                            lineWidth = int.Parse(xmlModel.ZkWidth);
                        }
                        catch { }

                        string zkl = double.Parse(dr["zkl"].ToString()).ToString("0.");

                        SizeF zklSize = e.Graphics.MeasureString(zkl, new Font(new FontFamily("宋体"), 9));
                        float fzklSize = seat + lineWidth - zklSize.Width;


                        e.Graphics.DrawString(dr["zkl"].ToString(), new Font(new FontFamily("宋体"), 9), System.Drawing.Brushes.Black, fzklSize, high);
                        slSite = seat;
                        seat += lineWidth;//数量 宽度 30
                    }

                    if (xmlModel.Zhj == "1")
                    {
                        int lineWidth = 30;

                        try
                        {
                            lineWidth = int.Parse(xmlModel.ZhjWidth);
                        }
                        catch { }

                        string zhj = double.Parse(dr["zhj"].ToString()).ToString("0.");

                        SizeF zhjSize = e.Graphics.MeasureString(zhj, new Font(new FontFamily("宋体"), 9));
                        float fzhjSize = seat + lineWidth - zhjSize.Width;


                        e.Graphics.DrawString(zhj, new Font(new FontFamily("宋体"), 9), System.Drawing.Brushes.Black, fzhjSize, high);
                        slSite = seat;
                        seat += lineWidth;//数量 宽度 30
                    }

                    if (xmlModel.Pfj == "1")
                    {
                        int lineWidth = 30;

                        try
                        {
                            lineWidth = int.Parse(xmlModel.PfjWidth);
                        }
                        catch { }

                        string pfj = double.Parse(dr["pfj"].ToString()).ToString("0.");

                        SizeF pfjSize = e.Graphics.MeasureString(pfj, new Font(new FontFamily("宋体"), 9));
                        float fpfjSize = seat + lineWidth - pfjSize.Width;


                        e.Graphics.DrawString(pfj, new Font(new FontFamily("宋体"), 9), System.Drawing.Brushes.Black, fpfjSize, high);
                        // slSite = seat;
                        seat += lineWidth;//数量 宽度 30
                    }
                    /////////////////////

                    if (xmlModel.Dj == "1")
                    {
                        int lineWidth = 30;

                        try
                        {
                            lineWidth = int.Parse(xmlModel.DjWidth);
                        }
                        catch { }

                        // e.Graphics.DrawString(dr["lsj"].ToString(), new Font(new FontFamily("宋体"), 9), System.Drawing.Brushes.Black, seat, high);
                        string lsj = double.Parse(dr["lsj"].ToString()).ToString("0.");
                        SizeF lsjSize = e.Graphics.MeasureString(lsj, new Font(new FontFamily("宋体"), 9));
                        float x1 = seat + lineWidth - lsjSize.Width;
                        e.Graphics.DrawString(lsj, new Font(new FontFamily("宋体"), 9), System.Drawing.Brushes.Black, x1, high);


                        seat += lineWidth;//单价 宽度 30
                    }
                    if (xmlModel.Xj == "1")
                    {
                        int lineWidth = 30;

                        try
                        {
                            lineWidth = int.Parse(xmlModel.XjWidth);
                        }
                        catch { }

                        //   e.Graphics.DrawString(dr["ssje"].ToString(), new Font(new FontFamily("宋体"), 9), System.Drawing.Brushes.Black, seat, high);

                        string ssje = double.Parse(dr["ssje"].ToString()).ToString("0.");

                        //if (ssje.IndexOf('.') == -1)
                        //{
                        //    ssje = ssje;
                        //}
                        SizeF ssjeSize = e.Graphics.MeasureString(ssje, new Font(new FontFamily("宋体"), 9));
                        float x3 = seat + lineWidth - ssjeSize.Width;
                        e.Graphics.DrawString(ssje, new Font(new FontFamily("宋体"), 9), System.Drawing.Brushes.Black, x3, high);



                        xjSite = x3;
                        seat += lineWidth;//商品名称宽度 30
                    }

                    high += 13;

                    rowIndex++;
                }

                e.Graphics.DrawString("合计：", new Font(new FontFamily("宋体"), 8, FontStyle.Bold), System.Drawing.Brushes.Black, 12, high);//合计行

                e.Graphics.DrawString(GetAllSl(spInfoData).ToString(), new Font(new FontFamily("宋体"), 8, FontStyle.Bold), System.Drawing.Brushes.Black, slSite, high);
                e.Graphics.DrawString(GetAllMoney(spInfoData).ToString("0.0"), new Font(new FontFamily("宋体"), 8, FontStyle.Bold), System.Drawing.Brushes.Black, xjSite, high);



                high += 13;

                e.Graphics.DrawLine(Pens.Black, 8, high, line, high);
                high = high + 10;//显示下分割线


                int key = 0;

                if (decimal.Parse(Ss) != 0)
                {
                    high = high + 20;//付现
                    e.Graphics.DrawString("实付：" + decimal.Parse(Ss).ToString("0.0"), new Font(new FontFamily("宋体"), 12, FontStyle.Bold), System.Drawing.Brushes.Black, 13, high);
                    key++;
                }
                int iHigh = 0;
                //具体支付方式
                string fkfs = GetZffs(ref iHigh);
                if (fkfs != "")
                {
                    high = high + 20;//付现
                    e.Graphics.DrawString("支付方式：" + fkfs, new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, 13, high);
                    key++;
                }
                high = high + iHigh + 5;

                string Glsf = "";//共计收费

                if (Hykxf != "")
                {
                    high = high + 20;//会员卡消费
                    e.Graphics.DrawString("会员卡消费：" + Hykxf, new Font(new FontFamily("宋体"), 12, FontStyle.Bold), System.Drawing.Brushes.Black, 13, high);
                    Glsf = (decimal.Parse(Hykxf) + decimal.Parse(Xj)).ToString();
                    key++;
                }

                if (key == 2)
                {
                    high = high + 20;//共计
                    e.Graphics.DrawString("共计：" + decimal.Parse(Glsf).ToString("0.0"), new Font(new FontFamily("宋体"), 12, FontStyle.Bold), System.Drawing.Brushes.Black, 13, high);
                }



                high = high + 20;//找零
                e.Graphics.DrawString("找零：" + decimal.Parse(Zl).ToString("0.0"), new Font(new FontFamily("宋体"), 12, FontStyle.Bold), System.Drawing.Brushes.Black, 13, high);
                high = high + 20;//收银员
                e.Graphics.DrawString("收银员：" + Syymc, new Font(new FontFamily("宋体"), 8, FontStyle.Bold), System.Drawing.Brushes.Black, 13, high);

                // e.Graphics.DrawString("营业员：" + NewYyymc, new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, 200, high);

                //string yyyStr = "营业员：" + NewYyymc;

                //SizeF yyyStrSize = e.Graphics.MeasureString(yyyStr, new Font(new FontFamily("宋体"), 8));
                //float yyySize = line - yyyStrSize.Width;

                //e.Graphics.DrawString(yyyStr, new Font(new FontFamily("宋体"), 8,FontStyle.Bold), System.Drawing.Brushes.Black, yyySize, high);

                if (xmlModel.PrintTime == "1")
                {
                    high = high + 15;//时间

                    e.Graphics.DrawString("时间：" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"), new Font(new FontFamily("宋体"), 8, FontStyle.Bold), System.Drawing.Brushes.Black, 12, high);
                }


                if (xmlModel.ctxtYw1 != "")
                {
                    high = high + 15;//时间
                    int width = GetYWSite(xmlModel.key1);
                    e.Graphics.DrawString(xmlModel.ctxtYw1, new Font(new FontFamily("宋体"), 8, FontStyle.Bold), System.Drawing.Brushes.Black, width, high);

                }
                if (xmlModel.ctxtYw2 != "")
                {
                    high = high + 15;//时间
                    int width = GetYWSite(xmlModel.key2);
                    e.Graphics.DrawString(xmlModel.ctxtYw2, new Font(new FontFamily("宋体"), 8, FontStyle.Bold), System.Drawing.Brushes.Black, width, high);
                }
                if (xmlModel.ctxtYw3 != "")
                {
                    high = high + 15;//时间
                    int width = GetYWSite(xmlModel.key3);
                    e.Graphics.DrawString(xmlModel.ctxtYw3, new Font(new FontFamily("宋体"), 8, FontStyle.Bold), System.Drawing.Brushes.Black, width, high);
                }
                if (xmlModel.ctxtYw4 != "")
                {
                    high = high + 15;//时间
                    int width = GetYWSite(xmlModel.key4);
                    e.Graphics.DrawString(xmlModel.ctxtYw4, new Font(new FontFamily("宋体"), 8, FontStyle.Bold), System.Drawing.Brushes.Black, width, high);
                }
                if (xmlModel.ctxtYw5 != "")
                {
                    high = high + 15;//时间
                    int width = GetYWSite(xmlModel.key5);
                    e.Graphics.DrawString(xmlModel.ctxtYw5, new Font(new FontFamily("宋体"), 8, FontStyle.Bold), System.Drawing.Brushes.Black, width, high);
                }
                if (xmlModel.ctxtYw6 != "")
                {
                    high = high + 15;//时间
                    int width = GetYWSite(xmlModel.key6);
                    e.Graphics.DrawString(xmlModel.ctxtYw6, new Font(new FontFamily("宋体"), 8, FontStyle.Bold), System.Drawing.Brushes.Black, width, high);
                }

            }
            catch
            {
                // MessageBox.Show(this, "打印出错！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }


        public void PrintXP(string dmmc, string kh, string yyymc, string czje, string zsje)
        {

            Dmmc = dmmc;
            Kh = kh;
            Yyymc = yyymc;
            Czje = czje;
            Zsje = zsje;




            //  xmlModel = GetXmlModel();
            xmlModel = common.GetPosConfig();

            int width = 400;
            int hight = 500;
            try
            {
                width = int.Parse(xmlModel.PageWidth);
                hight = int.Parse(xmlModel.PageHeight);


            }
            catch { }

            this.printDocument1.DefaultPageSettings.PaperSize = new PaperSize("Custum", width, hight);

            //根据对话框中输入的页边距设置打印文档的页边距
            printDocument1.DefaultPageSettings.Margins = new Margins(4, 20, 4, 20);            //设置打印文档的页面大小
            this.printDocument1.PrintPage += new PrintPageEventHandler(printDocument2_PrintPage);




            bool status = false;
            DataTable dt = ChanageData();
            foreach (DataRow dr in dt.Rows)
            {
                int number = 0;
                string name = dr["名称"].ToString();
                if (dr["次数"].ToString() != "")
                {
                    number = int.Parse(dr["次数"].ToString());
                }
                this.printDocument1.DefaultPageSettings.PrinterSettings.PrinterName = name;
                for (int i = 0; i < number; i++)
                {
                    try
                    {
                        this.printDocument1.Print();
                    }
                    catch (Exception ex)
                    {
                        Message.Warning(ex.ToString());
                        status = true;
                        break;
                    }
                }
                if (status)
                {
                    break;
                }
            }

        }

        private void printDocument2_PrintPage(object sender, PrintPageEventArgs e)
        {
            try
            {
                e.Graphics.DrawString("会员卡充值", new Font(new FontFamily("黑体"), 13), System.Drawing.Brushes.Black, 30, 10);
                e.Graphics.DrawString(Dmmc, new Font(new FontFamily("宋体"), 8), System.Drawing.Brushes.Blue, 140, 20);
                int topWidth = 15;

                int high = topWidth + 20;
                e.Graphics.DrawString("充值金额：" + Czje, new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, 12, high);

                if (Zsje != null && Zsje != "")
                {
                    high = topWidth + 20;
                    e.Graphics.DrawString("赠送金额：" + Zsje, new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, 12, high);
                }
                high = topWidth + 20;
                e.Graphics.DrawString("营业员  ：" + Yyymc, new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, 50, high);


            }
            catch
            {
                // MessageBox.Show(this, "打印出错！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }


        #endregion

        /// <summary>
        /// 支付方式
        /// </summary>
        /// <returns></returns>
        private string GetZffs(ref int high)
        {
            int size = 10;
            string str = "";
            if (Xj != "" && Xj != "0.00")
            {
                high += size;
                str = "\r\n现金:" + Xj;
            }

            if (Wx != "" && Wx != "0.00")
            {
                high += size;
                str = str + "\r\n微信:" + Wx;

            }


            if (Zfb != "" && Zfb != "0.00")
            {
                high += size;
                str = str + "\r\n支付宝:" + Zfb;

            }


            if (Yhk != "" && Yhk != "0.00")
            {
                high += size;

                str = str + "\r\n银行卡:" + Yhk;

            }

            if (Djq != "" && Djq != "0.00")
            {
                high += size;
                str = str + "\r\n代金券:" + Djq;


            }

            return str;
        }

        /// <summary>
        /// 数量合计
        /// </summary>
        /// <returns></returns>
        private decimal GetAllSl(DataTable dt)
        {
            decimal allSl = 0;
            decimal sl = 0;
            foreach (DataRow dr in dt.Rows)
            {
                if (dr["sl"].ToString() != "")
                    sl = decimal.Parse(dr["sl"].ToString());
                allSl += sl;
            }
            return allSl;
        }


        /// <summary>
        /// 实收金额合计
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        private decimal GetAllMoney(DataTable dt)
        {
            decimal allMoney = 0;
            decimal xj = 0;
            foreach (DataRow dr in dt.Rows)
            {
                if (dr["ssje"].ToString() != "")
                    xj = decimal.Parse(dr["ssje"].ToString());
                allMoney += xj;
            }

            //if (allMoney < 0)
            //    allMoney = 0;
            return allMoney;
        }

        /// <summary>
        /// 金额合计(未打折时)
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        private decimal GetWDZAllMoney(DataTable dt)
        {
            decimal allMoney = 0;
            decimal xj = 0;
            decimal je = 0;
            foreach (DataRow dr in dt.Rows)
            {
                if (dr["lsj"].ToString() != "")
                    xj = decimal.Parse(dr["lsj"].ToString());
                je = xj * decimal.Parse(dr["sl"].ToString());
                allMoney += je;
            }
            //if (allMoney < 0)
            //    allMoney = 0;
            return allMoney;
        }
        private string GetSpDlmc(string spbh)
        {
            BasicBLL basicBLL = new BasicBLL();
            //select dlmc,xlbh  from spdah  where spbh='000064'
            string dlmc = basicBLL.GetObjData(" select top 1 dlmc  from  spdah   where spbh='" + spbh + "' or xlbh='" + spbh + "'");
            return dlmc;
        }
        private string GetSpHh(string spbh)
        {
            BasicBLL basicBLL = new BasicBLL();

            string xlbh = basicBLL.GetObjData("  select xlbh  from  spdah  where spbh='" + spbh + "'  or xlbh='" + spbh + "'");
            return xlbh;
        }


        /// <summary>
        /// 获取页尾位置
        /// </summary>
        /// <param name="site">0 居左，1 居中，2 居右</param>
        /// <returns></returns>
        private int GetYWSite(string site)
        {
            int intSite = 0;
            if (site == "0")
            {
                intSite = 12;
            }
            else if (site == "0")
            {
                intSite = 120;
            }
            else
            {
                intSite = 250;
            }
            return intSite;

        }

        /// <summary>
        /// 获取xml 配置信息
        /// </summary>
        /// <returns></returns>
        private XmlModel GetXmlModel()
        {

            XmlDocument doc = new XmlDocument();
            string path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) + @"\PosConfig.xml";
            doc.Load(path);    //加载Xml文件  
            XmlElement rootElem = doc.DocumentElement;   //获取根节点  
            XmlModel model = new XmlModel();

            XmlNodeList InfoXmls = rootElem.ChildNodes;//继续获取  根节点 所有的子节点


            foreach (XmlNode xn1 in InfoXmls)//遍历
            {

                XmlElement xe = (XmlElement)xn1;//将子节点类型转换为XmlElement类型
                if (xe.GetAttribute("name") == "head")// 查找到表头
                {
                    XmlNodeList nls = xe.ChildNodes;
                    foreach (XmlNode xnn1 in nls)
                    {
                        XmlElement xe2 = (XmlElement)xnn1;

                        if (xe2.Name == "title")
                        {
                            model.Title = xe2.InnerText;

                        }
                        else if (xe2.Name == "printname")
                        {
                            model.PrintName = xe2.InnerText;

                        }
                        else if (xe2.Name == "printtime")
                        {
                            model.PrintTime = xe2.InnerText;
                        }
                        else if (xe2.Name == "site")
                        {
                            model.Site = xe2.InnerText;
                        }

                        else if (xe2.Name == "addinfo1")
                        {
                            model.Addinfo1 = xe2.InnerText;
                        }

                        else if (xe2.Name == "addinfo2")
                        {
                            model.Addinfo2 = xe2.InnerText;
                        }


                        else if (xe2.Name == "pageheight")
                        {
                            model.PageHeight = xe2.InnerText;
                        }

                        else if (xe2.Name == "pagewidth")
                        {
                            model.PageWidth = xe2.InnerText;
                        }


                    }


                }
                else if (xe.GetAttribute("name") == "body")// 查找到表体
                {
                    XmlNodeList nls = xe.ChildNodes;
                    foreach (XmlNode xnn1 in nls)
                    {
                        XmlElement xe2 = (XmlElement)xnn1;

                        if (xe2.Name == "ID")
                        {
                            model.ID = xe2.InnerText;
                            model.IDWidth = xe2.GetAttribute("Width");
                        }
                        else if (xe2.Name == "Spbh")
                        {
                            model.Spbh = xe2.InnerText;
                            model.SpbhWidth = xe2.GetAttribute("Width");
                        }
                        else if (xe2.Name == "Spmc")
                        {
                            model.Spmc = xe2.InnerText;
                            model.SpmcWidth = xe2.GetAttribute("Width");

                        }
                        else if (xe2.Name == "Sl")
                        {
                            model.Sl = xe2.InnerText;
                            model.SlWidth = xe2.GetAttribute("Width");

                        }
                        ////////////新增加的字段dw，gg，zk，zhj，pfj
                        else if (xe2.Name == "dw")
                        {
                            model.Dw = xe2.InnerText;
                            model.DwWidth = xe2.GetAttribute("Width");

                        }
                        else if (xe2.Name == "gg")
                        {
                            model.Gg = xe2.InnerText;
                            model.GgWidth = xe2.GetAttribute("Width");

                        }
                        else if (xe2.Name == "zk")
                        {
                            model.Zk = xe2.InnerText;
                            model.ZkWidth = xe2.GetAttribute("Width");

                        }
                        else if (xe2.Name == "zhj")
                        {
                            model.Zhj = xe2.InnerText;
                            model.ZhjWidth = xe2.GetAttribute("Width");

                        }
                        else if (xe2.Name == "pfj")
                        {
                            model.Pfj = xe2.InnerText;
                            model.PfjWidth = xe2.GetAttribute("Width");

                        }
                        ///////////////


                        else if (xe2.Name == "Dj")
                        {
                            model.Dj = xe2.InnerText;
                            model.DjWidth = xe2.GetAttribute("Width");

                        }
                        else if (xe2.Name == "Xj")
                        {
                            model.Xj = xe2.InnerText;
                            model.XjWidth = xe2.GetAttribute("Width");
                        }
                    }


                }
                else if (xe.GetAttribute("name") == "down")// 查找到表尾
                {
                    XmlNodeList nls = xe.ChildNodes;
                    foreach (XmlNode xnn1 in nls)
                    {

                        XmlElement xe2 = (XmlElement)xnn1;

                        if (xe2.Name == "ctxtYw1")
                        {
                            model.key1 = xe2.GetAttribute("key");
                            model.ctxtYw1 = xe2.InnerText;
                        }
                        if (xe2.Name == "ctxtYw2")
                        {
                            model.key2 = xe2.GetAttribute("key");
                            model.ctxtYw2 = xe2.InnerText;
                        }
                        if (xe2.Name == "ctxtYw3")
                        {
                            model.key3 = xe2.GetAttribute("key");
                            model.ctxtYw3 = xe2.InnerText;
                        }
                        if (xe2.Name == "ctxtYw4")
                        {
                            model.key4 = xe2.GetAttribute("key");
                            model.ctxtYw4 = xe2.InnerText;
                        }
                        if (xe2.Name == "ctxtYw5")
                        {
                            model.key5 = xe2.GetAttribute("key");
                            model.ctxtYw5 = xe2.InnerText;
                        }
                        if (xe2.Name == "ctxtYw6")
                        {
                            model.key6 = xe2.GetAttribute("key");
                            model.ctxtYw6 = xe2.InnerText;
                        }
                    }
                }
            }
            return model;
        }

        private DataTable ChanageData()
        {

            XmlDocument doc = new XmlDocument();
            string path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) + @"\config.xml";
            doc.Load(path);    //加载Xml文件  
            XmlElement rootElem = doc.DocumentElement;   //获取根节点  

            XmlNodeList personNodes = rootElem.GetElementsByTagName("PrintInfo"); //获取PrintInfo 子节点集合  

            string printName = "";
            string number = "";
            string type = "";


            DataTable dt = new DataTable();

            dt.Columns.Add("名称");
            dt.Columns.Add("次数");
            dt.Columns.Add("类型");

            foreach (XmlNode node in personNodes)
            {
                XmlElement xe = (XmlElement)node;//将子节点类型转换为XmlElement类型

                printName = xe.GetAttribute("PrintName");
                number = xe.GetAttribute("Number");//则修改打印次数
                type = xe.GetAttribute("Type");//则修改打印机类型

                DataRow dr = dt.NewRow();
                dr["名称"] = printName;
                dr["次数"] = number;
                dr["类型"] = type;
                dt.Rows.Add(dr);
            }
            return dt;
        }


    }
}
